<template>
  <el-dialog title="分配角色" :visible="showRoleDialog" @open="handleOpen" @close="handleClose">
    <!-- 角色列表 -->
    <el-checkbox-group v-model="selectedList" class="assign-role">
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <template #footer>
      <el-button type="primary" size="small">确定</el-button>
      <el-button size="small" @click="handleClose">取消</el-button>
    </template>
  </el-dialog>
</template>
<script>
import { reqGetRoleList } from '@/api/setting.js'
import { getDetailInfo } from '@/api/user'

export default {
  name: 'AssignRole',
  props: {
    showRoleDialog: {
      type: Boolean,
      required: true
    },
    currentUserId: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      // 选中的角色列表
      selectedList: [],
      // 所有的角色
      roleList: []
    }
  },
  methods: {
    // 处理弹窗打开
    async handleOpen () {
      // 调用接口获取所有的角色列表
      const ret = await reqGetRoleList({ page: 1, pagesize: 10000 })
      if (ret.code === 10000) {
        // 获取角色列表成功
        this.roleList = ret.data.rows
      }
      // 获取用户本来拥有的角色
      const ret2 = await getDetailInfo(this.currentUserId)
      if (ret2.code === 10000) {
        this.selectedList = ret2.data.roleIds
      }
    },
    // 处理弹窗关闭
    handleClose () {
      this.$emit('update:showRoleDialog', false)
    }
  }
}
</script>
<style lang="scss" scoped>
.assign-role {
  ::v-deep {
    .el-checkbox {
      font-size: 30px;
    }
  }
}
</style>
